package com.sdry.service.impl.hpf;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.hpf.SystemPropertyMapper;
import com.sdry.model.hpf.SystemProperty;
import com.sdry.service.hpf.SystemPropertyService;

/**
 * @ClassName: SystemPropertyServiceImpl 
 * @Description:系统属性Service实现
 * @Author hpf
 * @Date 2018年9月25日 下午3:11:19 
 * @version 1.0
 */
@Service
public class SystemPropertyServiceImpl implements SystemPropertyService {
	
	@Resource
	private SystemPropertyMapper systemPropertyMapper;

	/**
	 * 查询总数
	 * @param systemProperty
	 * @return
	 */
	public Integer countSystemProperty(SystemProperty systemProperty) {
		// TODO Auto-generated method stub
		return systemPropertyMapper.countSystemProperty(systemProperty);
	}

	/**
	 * 分页查询
	 * @param map
	 * @return
	 */
	public List<SystemProperty> listSystemProperty(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return systemPropertyMapper.listPageSystemProperty(map);
	}

	/**
	 * 新增系统属性
	 * @param systemProperty
	 * @return
	 */
	public Integer insertSystemProperty(SystemProperty systemProperty) {
		int isSuccess = -2;
		List<SystemProperty> list = systemPropertyMapper.getSystemPropertyByType(systemProperty.getType());
		if(list.size() == 0){
			int i = systemPropertyMapper.insertSystemProperty(systemProperty);
			if(i > 0){
				isSuccess = 2;
			}
		}
		return isSuccess;
	}

	/**
	 * 修改系统属性
	 * @param systemProperty
	 * @return
	 */
	public Integer updateSystemProperty(SystemProperty systemProperty) {
		int isSuccess = -3;
		SystemProperty property = systemPropertyMapper.getSystemPropertyById(systemProperty.getSysID());
		if(property.getType().equals(systemProperty.getType())){
			int i = systemPropertyMapper.updateSystemProperty(systemProperty);
			if(i > 0){
				isSuccess = 3;
			}
		}else{
			List<SystemProperty> list = systemPropertyMapper.getSystemPropertyByType(systemProperty.getType());
			if(list.size() == 0){
				int i = systemPropertyMapper.updateSystemProperty(systemProperty);
				if(i > 0){
					isSuccess = 3;
				}
			}
		}
		return isSuccess;
	}

	/**
	 * 删除系统属性
	 * @param sysID
	 * @return
	 */
	public Integer daleteSystemProperty(Long sysID) {
		// TODO Auto-generated method stub
		return systemPropertyMapper.deleteSystemProperty(sysID);
	}
	
	/**
	 * 批量删除系统属性
	 * @param idArr
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer daleteSystemPropertys(String [] idArr) {
		// TODO Auto-generated method stub
		int isSuccess = 0;
		try {
			for (int i = 0; i < idArr.length; i++) {
				isSuccess = systemPropertyMapper.deleteSystemProperty(Long.parseLong(idArr[i]));
			}
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}


}
